package template;

public class LinkedQueue<T> {
	static class Node<U>{
		U item;
		Node<U> next;
		Node() {
			this.item = null;
			this.next = null;
		}
		Node(U item,Node<U> next){
			this.item = item;
			if(next!=null) {
				next.next = this;
			}
		}
		boolean hasNext() {return next!=null;}
	}
	Node<T> header = null;
	Node<T> last = null;
	public void push(T value)
	{
		if(header == null) {
			Node<T> newNode = new Node<T>(value, null);
			header = last = newNode;
		}
		else {
			Node<T> newNode = new Node<T>(value, last);
			last = newNode;
		}
	}
	public T pop(){
		T result = header.item;
		if (header != last)
		{
			header = header.next;
		}
		else {
			header.item = null;
		}
		return result;
	}
	public static void main(String[] args){
		LinkedQueue<String> queue = new LinkedQueue<String>();
		for (String s : ("I am from china, I Am Chinese!").split(" "))
		{
			queue.push(s);
		}
		String s;
		while ( (s = queue.pop()) !=null)
		{
			System.out.println(s);
		}
	}
}
